package com.gadglet.data;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.gadglet.core.GadgletRequestWrapper;
import com.gadglet.data.utils.PMF;
import com.gadglet.params.ProfileFields;

/**
 * Utility class for DomainUserFriends  
 *
 */
public class DomainUserFriendsUtils {

	static Logger log = Logger.getLogger("DomainUserFriendsUtils");
	
	
	/**
	 * Add a new friend to the current Domain user
	 * @param request
	 * @return
	 */
	public static boolean addFreind(GadgletRequestWrapper request){
		
		DomainUserFriends myFriends=null;
		DomainUserFriends newFriend;
		
		// check security
		if(DomainUserUtils.getDomainUserByUniqueId(request.getParameter(ProfileFields.PROFILE_FRIEND_ID.getParamName()), request.getCurrentDomainUser().getAccount())==null)
			return false;
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.currentTransaction().begin();
		Query query = pm.newQuery(DomainUserFriends.class);
		
		
		
		try {

			query.setFilter("userUniqueId == userUniqueIdParam && friendUniqueId == friendUniqueIdParam ");
			query.declareParameters("String userUniqueIdParam, String friendUniqueIdParam ");
			query.setUnique(true);
			
			myFriends = (DomainUserFriends) query.execute(request.getCurrentUserId(),request.getParameter(ProfileFields.PROFILE_FRIEND_ID.getParamName()));
						
			if(myFriends==null){
			
				newFriend = new DomainUserFriends(request.getCurrentUserId(),request.getParameter(ProfileFields.PROFILE_FRIEND_ID.getParamName()));
			
				pm.makePersistent(newFriend);
				
				pm.currentTransaction().commit();
			
			}
			
				
					

		} catch (Exception e) {
			log.warning(e.getMessage());
		} finally {
			if (pm.currentTransaction().isActive())
				pm.currentTransaction().rollback();

			pm.close();
		}
		
		return true;	
	}
	
	/**
	 * @param request
	 * @return A list of friends for the current user 
	 */
	public static List <DomainUser> getMyfriends(GadgletRequestWrapper request){
		
	
		List <DomainUser> myFriendsList= null;
		
		List <DomainUserFriends> myFriends=null;
				
		PersistenceManager pm = PMF.get().getPersistenceManager();
	
		Query query = pm.newQuery(DomainUserFriends.class);
		query.setFilter("userUniqueId == userUniqueIdParam");
		query.declareParameters("String userUniqueIdParam");
		
		try {
			
			myFriends = (List<DomainUserFriends>) query.execute(request.getCurrentUserId());
			
			if (myFriends!=null){
				myFriendsList = new ArrayList <DomainUser>();
				for (DomainUserFriends friend:myFriends ){
					myFriendsList.add(DomainUserUtils.getDomainUserByUniqueId(friend.getFriendUniqueId(),request.getCurrentDomainUser().getAccount()));
				}
				
			}
		
		
		} catch (Exception e) {
			log.warning(e.getMessage());
		} finally {

			pm.close();
		}
		
	return myFriendsList;
		
	}

	
	/**
	 * Remove a friend from the current user friends list
	 * @param request
	 * @return
	 */
	public static boolean removeFriend(GadgletRequestWrapper request){
		
		
		DomainUserFriends myFriend=null;
	
		if(request.getParameter(ProfileFields.PROFILE_FRIEND_ID.getParamName()) == null)
			return false;
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.currentTransaction().begin();
		Query query = pm.newQuery(DomainUserFriends.class);
		
		try {
			

			query.setFilter("userUniqueId == userUniqueIdParam && friendUniqueId == friendUniqueIdParam ");
			query.declareParameters("java.lang.String userUniqueIdParam , java.lang.String friendUniqueIdParam ");
			query.setUnique(true);
			myFriend = (DomainUserFriends) query.execute(request.getCurrentUserId(),request.getParameter(ProfileFields.PROFILE_FRIEND_ID.getParamName()));
			
			if(myFriend!=null){
			
				pm.deletePersistent(myFriend);
				
				pm.currentTransaction().commit();
			}
			
		
		} catch (Exception e) {
		
			log.warning(e.getMessage());
		} finally {
			if (pm.currentTransaction().isActive())
				pm.currentTransaction().rollback();

			pm.close();
		}
		
		return true;
		
	}
	

	
}
